var dtime = '_deadtime';

function setStorageSync(k, v) {
  this.setStorageSyncT(k, v, 0)
}

function setStorageSyncT(k, v, t) {
  wx.setStorageSync(k, v)
  var seconds = parseInt(t);
  if (seconds > 0) {
    var timestamp = Date.parse(new Date());
    timestamp = timestamp / 1000 + seconds;
    wx.setStorageSync(k + dtime, timestamp + "")
  } else {
    wx.removeStorageSync(k + dtime)
  }
}
 
function getStorageSync(k, def) {
  var deadtime = parseInt(wx.getStorageSync(k + dtime))
  if (deadtime) {
    if (parseInt(deadtime) < Date.parse(new Date()) / 1000) {
      if (def) { return def; } else { return; }
    }
  }
  var res = wx.getStorageSync(k);
  if (res) {
    return res;
  } else {
    return def;
  }
}
 
function removeStorageSync(k) {
  wx.removeStorageSync(k);
  wx.removeStorageSync(k + dtime);
}
 
function clearStorageSync() {
  wx.clearStorageSync();
}
 
module.exports = {
  setStorageSyncT: setStorageSyncT,
  setStorageSync: setStorageSync,
  getStorageSync: getStorageSync,
  removeStorageSync: removeStorageSync,
  clearStorageSync: clearStorageSync,
}